<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

class WowTable
{
	static $id_element = 1;
	
	private $title;
	private $structure = array();
	private $columns = array();
	
	function WowTable($title)
	{
		$this->title = $title;
	}
	
	function AddSubsection($section=null)
	{
		if ($section == null)
		{
			array_push($this->structure, array());
		}
		elseif ( !array_key_exists($section, $this->structure) )
		{
			$this->structure[$section] = array();
		}
	}
	
	function HasSubSection( $section )
	{
		return array_key_exists($section, $this->structure);
	}
	
	function AddColumn($name, $align=null, $size=null, $title_centered=false)
	{
		array_push($this->columns, array(
			'name' => $name,
			'size' => $size,
			'align' => $align,
			'title_centered' => $title_centered,
		));
	}
	
	function AddLine($section, $line)
	{
		array_push($this->structure[$section], $line);
	}
	
	function GetAndPrintTable($sort_it = false)
	{
		$n_cols = count($this->columns);
		
		echo "<table cellspacing='0' class='wowtable'>";
		echo "<tr>";
		echo "<td class='headerLeft'></td>";
		echo "<td colspan='".$n_cols."' class='headerCenter'><strong>".$this->title."</strong></td>";
		echo "<td class='headerRight'></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='borderTopLeft'></td>";
		echo "<td colspan='".$n_cols."' class='borderTop'></td>";
		echo "<td class='borderTopRight'></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='borderLeft'></td>";
		
		foreach ($this->columns as $v)
		{
			echo "<td class='subHeader' style='";
			
			if ($v['title_centered'] == true)
			{
				echo "text-align: center; ";
			}
			
			if ($v['size'] != null)
			{
				echo "width: ".$v['size']."px; ";
			}
			
			echo "'>".$v['name']."</td>";
		}
		
		echo "<td class='borderRight'></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='borderLeft'></td>";
		echo "<td colspan='".$n_cols."' class='subHeaderDecoration'></td>";
		echo "<td class='borderRight'></td>";
		echo "</tr>";
		
		if ($sort_it == true)
		{
			ksort($this->structure);
		}
		
		$line_type = 0;
			
		foreach ($this->structure as $struct_name => $struct_data)
		{
			if ( !is_numeric($struct_name) )
			{
				echo "<tr>";
				echo "<td class='borderLeft'></td>";
				echo "<td colspan='".$n_cols."' class='seperator' onclick=\"javascript:toggle('".self::$id_element."', '".count($struct_data)."');\">";
				echo "<img src='Interface/Buttons/UI-PlusButton-Up.gif' alt='' id='plus_minus_img_".self::$id_element."' style='vertical-align: middle;' /> ";
				echo $struct_name;
				echo " (";
				echo count($struct_data);
				echo ")";
				echo "</td>";
				echo "<td class='borderRight'></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td class='borderLeft'></td>";
				echo "<td colspan='".$n_cols."' class='subHeaderDecoration'></td>";
				echo "<td class='borderRight'></td>";
				echo "</tr>";
			}
			
			foreach ($struct_data as $line)
			{
				echo "<tr id='toggable_line_".self::$id_element."' style='".( is_numeric($struct_name) ? null : "display: none;")."'>";
				echo "<td class='borderLeft'></td>";
				
				foreach ($line as $column_i => $column)
				{
					echo "<td class='lineType".$line_type."' style='";
					
					if ( $this->columns[$column_i]['align'] != null)
					{
						echo "text-align: ".$this->columns[$column_i]['align'].";";
					}
					
					echo "'>".$column."</td>";
				}
				
				echo "<td class='borderRight'></td>";
				echo "</tr>";
				
				self::$id_element = self::$id_element + 1;
				
				if ($line_type == 0)
				{
					$line_type = 1;
				}
				else
				{
					$line_type = 0;
				}
			}
		}
		
		echo "<tr>";
		echo "<td class='borderBotLeft'></td>";
		echo "<td colspan='".$n_cols."' class='borderBot'></td>";
		echo "<td class='borderBotRight'></td>";
		echo "</tr>";
		echo "</table>";
	}
}

?>